package com.example.idlefish.MyAccount.activity

import android.os.*
import android.util.Log
import androidx.activity.*
import com.example.idlefish.MyAccount.contract.MeFansContract
import com.example.idlefish.MyAccount.contract.MeFollowContract
import com.example.idlefish.MyAccount.contract.MeInformationContract
import com.example.idlefish.databinding.*
import com.fasterxml.jackson.databind.*
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import com.example.idlefish.MyAccount.pojo.*
import com.example.idlefish.PublicModule.UserConst

class MyHomepageActivity : ComponentActivity() {
    private lateinit var binding: MyaccountHomepageBinding
    private val client = OkHttpClient()
    private val mapper = ObjectMapper()
    private var signInId = 0
    private val handler = Handler(Looper.getMainLooper())
    private val mediaType = "application/json;charset=utf-8".toMediaTypeOrNull()
    private val message = Message()
    private lateinit var userInfo: UserHomepageInfo

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        binding = MyaccountHomepageBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initInfo()


        binding.EditData.setOnClickListener {
            userInfo.let { it1 -> meInformationLauncher.launch(it1) }
        }
        binding.follow.setOnClickListener {
            meFollowLauncher.launch(userInfo.id)
        }
        binding.fans.setOnClickListener{
            meFansLauncher.launch(userInfo.id)
        }
    }

    private fun initInfo() {
        userInfo = CommonUtil.getSerializable(intent, "myHomepageInfo", UserHomepageInfo::class.java)
        Log.i("个人主页信息", "$userInfo")
        binding.nickname.text = userInfo.nickname
        binding.avatar.setImageBitmap(UserConst.avatarImage)
        binding.introduction.text = userInfo.intro
        binding.followNum.text = userInfo.followToIds!!.size.toString()
        binding.fansNum.text = userInfo.fansIds!!.size.toString()
    }

    // 设置信息
    private val meInformationLauncher = registerForActivityResult(MeInformationContract()) { result ->
        binding.nickname.text = result?.nickname
        userInfo.nickname = result?.nickname.toString()
        userInfo.intro = result?.intro.toString()
        userInfo.sex = result?.sex ?: 1

        Log.i("修改信息返回的", "$result")
    }



    private val meFollowLauncher = registerForActivityResult(MeFollowContract()) { result ->
    }

    private val meFansLauncher = registerForActivityResult(MeFansContract()) { result ->
    }

}
